#include "struct.h"

void estvraie(node *abr,char *str)
{
  
  int k = monstrlen(str);
  str = suprpar(str, 0, k);
  printf("%s\n", str);
  if(!checkxpr(str))
    printf ("caca !");
  int j = 0;
  int i = 0;
  char *str1 = malloc(20*sizeof(char));
  char *str2 = malloc(20*sizeof(char));
  char *op = malloc(3*sizeof(char));
  while((str[i] == '(')||(str[i]==')'))
    i++;

  while(checkcaract(str[i]))
    {
      str1[j] = str[i];
      i++;
      j++;
    }
  j=0;
  while((str[i] == '(')||(str[i]==')'))
    i++;

  if(checksymbs(str[i])||checksymbd(str[i]))
    {
      op[0] = str[i];
      i++;  
      op[1]='\0';
    }
  if(checksymbd(str[i]))
    {
      op[1] = str[i+1];
      op[2]='\0';
      i++;
    }
  while((str[i] == '(')||(str[i]==')'))
    i++;

  while(checkcaract(str[i]))
    {
      str2[j] = str[i];
      i++;
      j++;
    }

  return(validexp(str1,op,str2)};
}

int validexp(char *str11,char *op,char *str21)
{

  node *tmp1 = recherche(abr,str11);
  node *tmp2 = recherche(abr,str21);

  if(!strcmp("ent", tmp11->type))
    int str1 = returnint(tmp1);
  if(!strcmp("ent", tmp21->type))
    int str2 = returnint(tmp);
  if(!strcmp("car", tmp11->type))
    car str1 = returncar(tmp1);
  if(!strcmp("car", tmp21->type))
    car str1 = returncar(tmp1);

  if(!strcmp(op,"=="))
    {
      return(str1 == str2);
    }
  else if(!strcmp(op,"!="))
    {
      return(str1 != str2);
    }
  else   if(!strcmp(op,"&&"))
    {
      return(str1 == str2);
    }
  else   if(!strcmp(op,"<"))
    {
      return(str1 < str2);
    }
  else   if(!strcmp(op,"<="))
    {
      return(str1 >= str2);
    }
  else   if(!strcmp(op,">"))
    {
      return(str1 > str2);
    }
  else   if(!strcmp(op,">="))
    {
      return(str1 >= str2);
    }
  else 
    {
      return(-4242);
    }

}

char *suprpar(char *str, int r,int k)
{
  int z;
  char *tmp = malloc(256 * sizeof(char));
  for(z = 0 ; z <= k; z++)
    {
      if((str[z] != ' '))
	{
	  tmp[r] = str[z];
	  r++;
	}
    }
  return tmp;
}
//renommer en estBooleen(char *str)

int estBooleen(char *str)
{
  str = suprparentheses(str);
  int l = strlen(str);
  int i;
  for(i=0;(i<l)&&(checkcaract(str[i]));i++){}
  for(i=0;(i<l)&&(checksymbole(str[i]));i++){}
  for(i=0;(i<l)&&(checkcaract(str[i]));i++){}
  return l == i;
}

char *suprarentheses(char *str)
{
  int ij = 0;
  char *tmp = malloc(strlen(str)*(sizeof(char)));
  for(i=0;i<l;i++)
    {
      if(str[i]!=')' || str[i]!='(')
	{
	  tmp[j]=str[i];
	  j++;
	}
    }
}
int main (void)
{
  char *str = ("((  greymon > patamon ))")
    cheapestbool(str);
  /*
    ab *hey = malloc(sizeof(struct abh));
    char *str = ("((  a > b ) || ( c == f ) && ( ( a * b ) < c))");
    int k = monstrlen(str);
    str = suprpar(str, 0, k);
    printf("%s    %d   %d\n", str, checkcaract('>'),checksymbole('>'));
    if(!checkxpr(str))
    printf ("caca !");
    hey = estBooleen(str);
    afficher(hey);*/
  return 0;
}


int monstrlen(char*v)
{
  int z;
  for(z=0;v[z] != '\0'; z++)
    {

    }
  if(z==0)
    return 0;

  return(z);
}
char* monstrcat(char*v,char*w)
{
  int z;
  z=monstrlen(v);
  if(z==0)
    {
      v[0]=w[0];
      v[1]='\0';
    }
  else
    {
      v[z]=w[0];
      v[z+1]='\0';
    }
  return v;

}

int checkxpr(char *str)
{
  int h = 0;
  int l = monstrlen(str);
  while((h<l) && ((str[h]=='(')||(str[h]==')')||checksymbole(str[h])||checkcaract(str[h])))
    {
      h++;
    }
  return (h == l);
}

int checkcaract(char c)
{
  return(((c>='a')&&(c<='z'))||((c >='A')&&(c<='Z'))||((c>='0')&&(c<='9')));
}

int checksymbole(char c)
{
  return((c=='+')||(c=='-')||(c=='*')||(c=='/')||(c =='|')||(c=='&')||(c=='<')||(c=='>')||(c=='=')||(c=='!'));
}

int checksymbs(char c)
{
  return( (c=='+') || (c=='-') || (c=='*') || (c=='/') );
}
int checksymbd(char c)
{
  return( (c =='|') || (c=='&') || (c=='<') || (c=='>') || (c=='=') || (c=='!'));
}



ab *estBoolen(char *str)
{
  int i = 0;
  int *pi = &i;
  int j = 0;
  int *pj = &j;

  ab *abool=malloc(sizeof(abool));
  int k = monstrlen(str);

  if(abool == NULL)
    return 0;
  printf("%s", str);
  printf("\n");
  abool = fusion(str,k, pi,pj);
  return(abool);

}
/*int i : position du curseur
//int j : nombres de parentheses
//int l : strlen(str)
//int p : position dans la chaine*/

ab *fusion(char *str, int l,int *i,int *j)
{
  ab *tmp = malloc(sizeof(ab));
  tmp->op = malloc(12*sizeof(char));
  tmp->op[0]='\0';
  char *str2 = malloc(12*sizeof(char));
  int q = 0;
  int y = 0;
  if(l == -1)
    {
      printf("coucou");
      tmp->fg = NULL;
      tmp->fd = NULL;
      tmp->op = strcat(tmp->op, str);
      return tmp;
    }
  else
    {
      while(*i < l)
	{
	  if(str[*i] == '(')
	    {
	      printf("fg ");	      
	      *i=*i+1;
	      *j=*j+1;
	      fusion(str, l, i,j);
	    }
	  if(checkcaract(str[*i]))
	    {
	      while((*i<l) && checkcaract(str[*i]))
		{
		  printf("1");
		  str2[q] = str[*i];
		  q++;
		  *i=*i+1;
		}
	      str2[q+1] = '\0';
	      //printf("\'%d\'",i);
	      tmp->fg = fusion(str2, -1,i,j);
	      q = 0;
	    }
	  if(checksymbs(str[*i]))
	    {
	      printf("2");
	      tmp->op[0] = str[*i];
	      *i=*i+1;  
	      tmp->op[1]='\0';
	    }
	  if(checksymbd(str[*i]))
	    {
	      printf("3");
	      tmp->op[1] = str[*i];
	      tmp->op[2]='\0';
	      *i=*i+1;
	    }
	}
      if(checkcaract(str[*i]))
	{
	  while((*i<l) && checkcaract(str[*i]))
	    {
	      printf("6");
	      str2[q] = str[*i];
	      q++;
	      *i=*i+1;
	    }
	  str2[q]='\0';
	  q = 0;
	  tmp->fd = fusion(str2,-1,i,j);
	}
      if(str[*i] == ')')
	{
	  printf("7");
	  *i=*i+1;
	  *j=*j-1;
	  if(!j)
	    {
	      *i=*i+1;
	      printf("8");
	      return tmp;
	    }
	  if(checkcaract(str[*i]))
	    {
	      while((*i<l)&&checkcaract(str[*i]))
		{
		  printf("9");
		  str2[q] = str[*i];
		  q++;
		  *i=*i+1;
		}
	      str2[q+1]='\0';
	      q = 0;
	      tmp->fg = fusion(str2,-1,i,j);
	    }
	  if(checksymbs(str[*i]))
	    {
	      printf("10");
	      tmp->op[0] = str[*i];
	      tmp->op[1]='\0';
	      *i=*i+1;
	    }
	  if(checksymbd(str[*i]))
	    {
	      printf("11");
	      tmp->op[1] = str[*i];
	      *i=*i+1;
	      tmp->op[2] = '\0';
	    }
	  while((*i<l)&&checkcaract(str[*i]))
	    {
	      printf("fd");
	      *i=*i+1;
	      tmp->fd = fusion(str,l,i,j);
	    }
	  *i=*i+1;
	}
      return tmp;
    }
  printf("fin");
  return tmp;
}

void afficher(ab *hey)
{
  if(hey != NULL)
    {
      if(hey->fg != NULL)
	afficher(hey->fg);

      printf("%s",hey->op);

      if(hey->fd != NULL)
	afficher(hey->fd);
    }
  else
    {
      printf("C'est vide connard, ton algo ne marche pas");
    }
}
